package com.xnyzc.lhy.didi.mq;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xnyzc.lhy.common.component.config.security.RocketConfig;
import com.xnyzc.lhy.common.constant.MqConstant;
import com.xnyzc.lhy.common.constant.MqConsumerGroupConstant;
import com.xnyzc.lhy.didi.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.didi.mapper.order.OaCOrderPersonalMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xnyzc.lhy.didi.entity.order.OaCOrderPersonal;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class UpdateOrderMQConsumer {
    /**
     * rocketMq 配置文件
     */
    @Autowired
    RocketConfig rocketConfig;


    private DefaultMQPushConsumer consumer;

   @Autowired
   private OaCOrderPersonalMapper oaCOrderPersonalMapper;


//    @Value("${rocketMq.isStart}")
//    private boolean isStart;

    /**
     * 开启监听
     */
    @PostConstruct
    public void init() {
        log.info(rocketConfig.getTopic() + "--------------------------------------------------" + rocketConfig.getNameServerAddr());
//        if (!isStart) {
//            return;
//        }
        try {
            consumer = new DefaultMQPushConsumer(MqConsumerGroupConstant.UPDATE_PASSENGER_INFO);
            consumer.setNamesrvAddr(rocketConfig.getNameServerAddr());
            consumer.subscribe(rocketConfig.getTopic(), MqConstant.UPDATE_PASSENGER_INFO);
            //批量消费的最大消息条数
            consumer.setConsumeMessageBatchMaxSize(10);
            //设置消费策略:队列头消费
            consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
            /**
             *  设置监听
             */
            consumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    for (MessageExt msg : msgs) {
                        UpdateOrderMQConsumer.this.dealBody(msg);
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            consumer.start();
            log.info("---------UpdateOrderMQConsumer 启动完成------------");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("---------UpdateOrderMQConsumer 初始化失败------------");
        }
    }

    private void dealBody(MessageExt messageExt) {
        String data = new String(messageExt.getBody());
        log.info("UpdateOrderMQConsumer收到MQ的消息为-" + data);
        OaCOrderPersonal personal = JSON.parseObject(data, OaCOrderPersonal.class);
        if (personal == null) {
            return;
        }
        UpdateWrapper<OaCOrderPersonal> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set(OaCOrderPersonal.PASSENGER_NAME,personal.getPassengerName());
        updateWrapper.set(OaCOrderPersonal.PASSENGER_PHONE,personal.getPassengerPhone());
        updateWrapper.set(OaCOrderPersonal.PASSENGER_PHONE_SUFFIX,personal.getPassengerPhoneSuffix());
        updateWrapper.set(OaCOrderPersonal.UPDATE_USER,personal.getPassengerName());
        updateWrapper.set(OaCOrderPersonal.UPDATE_TIME,new Date());
        updateWrapper.eq(OaCOrderPersonal.PERSONAL_ID,personal.getPersonalId());
        int update = oaCOrderPersonalMapper.update(null, updateWrapper);
        if (update>0){
            log.info("订单：[{}] 更新乘客信息成功！",personal.getOrderNo());
        }else{
            log.info("订单：[{}] 更新乘客信息失败！",personal.getOrderNo());
        }
    }
}
